#include "uart.c"
#include "timer.c"
#include <regx52.h>

// LED端口定义
#define LED_PORT P2
// 按键端口定义
#define KEY_PORT P1

// 全局变量定义
bit game_started = 0;      // 游戏是否开始标志
bit game_paused = 0;       // 游戏暂停标志
bit game_over = 0;         // 游戏结束标志
unsigned char current_led = 0; // 当前点亮的LED
unsigned int timer_count = 0;  // 定时器中断计数
unsigned int threshold = 40;   // 超时阈值（增加难度）
unsigned char pause_led = 0;   // 暂停状态流水灯位置
unsigned char flash_state = 0; // 闪烁状态
unsigned int flash_count = 0;  // 闪烁计数
unsigned char i;
void delay(unsigned int ms);
unsigned char get_random_led(void);
bit check_game_over(unsigned char led);
void start_game(void);
void pause_game(void);
void flashing_light(void);
void flowing_light(void);

sbit START_STOP_KEY = P3^0;

void timer0_isr() interrupt 1 {
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;
    
    if (game_started && !game_paused && !game_over) {
        timer_count++;
        if (timer_count >= threshold) {
            game_over = check_game_over(current_led);
        }
    }
    
    if (flash_count < 0xFFFF) flash_count++;
}

void main() {

    uart_init();

    timer0_init();
    EA = 1;
    
    LED_PORT = 0xFF; // 初始熄灭所有LED
    
    while(1) {
        // 检测开始/暂停按键
        if (START_STOP_KEY == 0) {
            delay(20); 
            if (START_STOP_KEY == 0) {
                while(!START_STOP_KEY); // 等待按键释放
                
                if (!game_started) {
                    start_game(); 
                } else {
                    pause_game(); 
                }
            }
        }
        
        // 游戏结束时的LED闪烁
        if (game_over) {
            flashing_light();
        }

        else if (game_paused) {
            flowing_light();
        }

        else if (game_started && !game_over) {
            unsigned char key_val = ~KEY_PORT; 
            
            if (key_val) { // 有按键按下
                delay(20); 
                key_val = ~KEY_PORT;
                if (key_val) {
                    // 找出按下的按键
                    for (i = 0; i < 8; i++) {
                        if (key_val & (1 << i)) {
                            // 如果按下正确按键
                            if (i == current_led) {
                                LED_PORT = 0xFF; // 熄灭当前LED
                                current_led = get_random_led(); // 获取新LED
                                LED_PORT = ~(1 << current_led); // 点亮新LED
                                timer_count = 0; 
                            } else {
                                // 按错按键游戏结束
                                game_over = 1;
                            }
                            while((~KEY_PORT) != 0); // 等待按键释放
                            break;
                        }
                    }
                }
            }
        }
    }
}

void delay(unsigned int ms) {
    unsigned int i, j;
    for (i = 0; i < ms; i++)
        for (j = 0; j < 123; j++);
}

unsigned char get_random_led(void) {

    unsigned int seed = (TH0 << 8) | TL0;
    return (seed % 8);
}

bit check_game_over(unsigned char led) {

    return 1;
}

void start_game(void) {
    game_started = 1;
    game_paused = 0;
    game_over = 0;
    timer_count = 0;
    current_led = get_random_led();
    LED_PORT = ~(1 << current_led); // 点亮随机LED
}

void pause_game(void) {
    game_paused = !game_paused;
    
    if (game_paused) {
        LED_PORT = 0xFF; 
        pause_led = 0;   // 重置流水灯位置
        flash_count = 0;  
    } else {
        LED_PORT = ~(1 << current_led); 
    }
}

void flashing_light(void) {
  
    if (flash_count >= 4) { 
        flash_count = 0;
        flash_state = !flash_state;
        LED_PORT = flash_state ? ~(1 << current_led) : 0xFF;
    }
}

void flowing_light(void) {
  
    if (flash_count >= 4) { 
        flash_count = 0;
        LED_PORT = ~(1 << pause_led);
        pause_led = (pause_led + 1) % 8; 
    }
}

